package com.htfyun.testa2recorder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Process;

import androidx.appcompat.app.AppCompatActivity;

import com.htfyun.testa2recorder.databinding.ActivityTestMultiMicBinding;
import com.htfyun.testa2recorder.utils.DebugLog;
import com.htfyun.testa2recorder.utils.executor.AppExecutors;

public class MyBroadcastReceiver extends BroadcastReceiver {

    private final ActivityTestMultiMicBinding binding;
    private final AppCompatActivity activity;
    private final int MAX_RECORD_DURATION = 5 * 60 * 1000;
    public MyBroadcastReceiver(ActivityTestMultiMicBinding binding, AppCompatActivity activity) {
        this.binding = binding;
        this.activity = activity;
    }

    public void registerMe() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ReceiverCommand.ACTION_RECORDER_START);
        filter.addAction(ReceiverCommand.ACTION_RECORDER_STOP);
        filter.addAction(ReceiverCommand.ACTION_RECORDER_FINISH);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            activity.registerReceiver(this, filter, Context.RECEIVER_EXPORTED);
        } else {
            activity.registerReceiver(this, filter);
        }
    }

    public void unregisterMe() {
        activity.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        final String action = intent.getAction();
        DebugLog.e("onReceive action = " + action);
        if (action == null) {
            return;
        }
        switch (action) {
            case ReceiverCommand.ACTION_RECORDER_START:
                AppExecutors.removeFromMainThread(timeoutRunnable);
                stopRecorder();

                int duration = intent.getIntExtra(ReceiverCommand.EXTRA_RECORD_DURATION, Integer.MAX_VALUE);
                duration = Math.min(MAX_RECORD_DURATION, duration);
                if (duration < 0) {
                    duration = MAX_RECORD_DURATION;
                }
                if (duration > 0) {
                    AppExecutors.postToMainThreadDelay(timeoutRunnable, duration);
                }
                startRecorder();
                break;
            case ReceiverCommand.ACTION_RECORDER_STOP:
                AppExecutors.removeFromMainThread(timeoutRunnable);
                stopRecorder();
                break;
            case ReceiverCommand.ACTION_RECORDER_FINISH:
                AppExecutors.removeFromMainThread(timeoutRunnable);
                stopRecorder();
                activity.finish();
                Process.killProcess(Process.myPid());
                break;
        }
    }

    private final Runnable timeoutRunnable = this::stopRecorder;

    private void startRecorder() {
        if (binding != null) {
            TestMultiMicActivity.MicArrayUtils.setMicArrayGain(activity, 1);
            binding.cbMicArray.setChecked(true);
            binding.start.performClick();
        }
    }

    private void stopRecorder() {
        if (binding != null) {
            binding.stop.performClick();
            TestMultiMicActivity.MicArrayUtils.setMicArrayGain(activity, 10);
        }
    }

}
